<?php

namespace app\controller\v1;

use app\model\ProductCategoryModel;
use app\validate\ProductCategoryValidate;

class ProductCategory extends System
{

    /**
     * Notes: 树形产品分类
     * User: chenLei
     * Date: 9/25/25
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function treeList()
    {
        $productCategory = new ProductCategoryModel();
        $res = $productCategory->treeList($this->params);
        return $this->successJson($res);
    }

    /**
     * Notes: 顶级产品分类
     * User: chenLei
     * Date: 9/25/25
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function topList()
    {
        $productCategory = new ProductCategoryModel();
        $res = $productCategory->topList();
        return $this->successJson($res);
    }

    /**
     * Notes: 添加产品分类
     * User: chenLei
     * Date: 9/25/25
     * @return \think\response\Json
     */
    public function add()
    {
        $validate = new ProductCategoryValidate();
        if (!$validate->scene('add')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $productCategory = new ProductCategoryModel();
        $res = $productCategory->add($this->params);

        return $this->successJson();
    }


    /**
     * Notes: 编辑分类
     * User: chenLei
     * Date: 9/25/25
     * @return \think\response\Json
     */
    public function edit()
    {
        $validate = new ProductCategoryValidate();
        if (!$validate->scene('edit')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $productCategory = new ProductCategoryModel();
        $res = $productCategory->edit($this->params);
        if ($res === false) {
            return $this->errorJson($productCategory->getError());
        }

        return $this->successJson();
    }


    /**
     * Notes: 编辑分类
     * User: chenLei
     * Date: 9/25/25
     * @return \think\response\Json
     */
    public function del()
    {
        $validate = new ProductCategoryValidate();
        if (!$validate->scene('del')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $productCategory = new ProductCategoryModel();
        $res = $productCategory->del($this->params);
        if ($res === false) {
            return $this->errorJson($productCategory->getError());
        }

        return $this->successJson();
    }
}